/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.service.CacheService;
import com.gitee.fufu669.utils.CacheAopUtil;
import com.gitee.fufu669.utils.CacheHtmlUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

// 注解
@Lazy
// 注解
@Aspect
// 组件注解
@Component
/** @author wangfupeng */
/**
 * TODO
*/
public class MockExecuteLaterAop {

    /**
     * TODO
    */
    public static final Logger logger = LoggerFactory.getLogger(MockExecuteLaterAop.class);

    // 注解
    @Autowired
    // 注解
    @Qualifier(value = "threadPoolTaskExecutorCacheFetchLater")
    // 定义
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    // 注解
    @Autowired
    // 定义
    CacheService cacheService;

    // 注解
    @Around("@annotation(com.gitee.fufu669.aspect.MockExecuteLater)")
    /**
     * TODO
    */
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 赋值
        int secondsBetweenExecutions = CacheKeyCommon.MOCKEXECUTELATER_DEFAULT_SECONDS_BETWEEN_EXECUTIONS;
        // 赋值
        String methodName = null;
        // 赋值
        String description = "";
        // 尝试下面的代码
        try {
            // 字符串赋值
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            // TODO
            methodName = proceedingJoinPoint.getSignature().getName();
            // TODO
            Object[] arguments = proceedingJoinPoint.getArgs();
            // TODO
            Class targetClass = Class.forName(targetName);
            // TODO
            Method[] methods = targetClass.getMethods();
            // 遍历
            for (Method method : methods) {
                // 判断
                if (method.getName().equals(methodName)) {
                    // TODO
                    secondsBetweenExecutions = method.getAnnotation(MockExecuteLater.class).secondsBetweenExecutions();
                    // TODO
                    description = method.getAnnotation(MockExecuteLater.class).description();
                    // 跳出
                    break;
                }
            }
        // 捕捉异常
        } catch (Exception e) {
            logger.info(e.toString());
        }
        // 赋值
        String mockExecuteLaterCacheKey = "mockexecutelater:" + CacheAopUtil.getCacheKey(proceedingJoinPoint);
        // 判断
        if (CacheAopUtil.isRefreshFlag(secondsBetweenExecutions, mockExecuteLaterCacheKey, cacheService) == true) {
            // 创建
            CustomThreadToRunAndSaveToRedis customThreadToRunAndSaveToRedis = new CustomThreadToRunAndSaveToRedis();
            // TODO
            customThreadToRunAndSaveToRedis.setProceedingJoinPoint(proceedingJoinPoint);
            // TODO
            customThreadToRunAndSaveToRedis.setDescription(description);
            // 执行
            threadPoolTaskExecutor.execute(customThreadToRunAndSaveToRedis);
        }
        // 返回
        return null;
    }

    /**
     * TODO
    */
    public class CustomThreadToRunAndSaveToRedis implements Runnable {
        // TODO
        private ProceedingJoinPoint proceedingJoinPoint;
        // 赋值
        private String description = "";

        /**
         * TODO
        */
        public void setProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
            // 赋值
            this.proceedingJoinPoint = proceedingJoinPoint;
        }

        /**
         * TODO
        */
        public void setDescription(String description) {
            // 赋值
            this.description = description;
        }

        /**
         * TODO
        */
        public void run() {
            // 字符串赋值
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            // 字符串赋值
            String methodName = proceedingJoinPoint.getSignature().getName();
            logger.info("%%%%%%MOCKEXECUTELATERAop:multiThread start:Classname:" + CacheHtmlUtil.getLastStringSplitHtml(targetName, "\\.") + ":methodName:" + methodName + ":description:" + description + "%%%%%%");
            // 赋值
            Object value = null;
            // 尝试下面的代码
            try {
                // TODO
                value = proceedingJoinPoint.proceed();
            // 捕捉异常
            } catch (Throwable throwable) {
                logger.info(throwable.getMessage(), throwable);
            // 最终执行
            } finally {
                logger.info("%%%%%%MOCKEXECUTELATERAop:multiThread end:Classname:" + CacheHtmlUtil.getLastStringSplitHtml(targetName, "\\.") + ":methodName:" + methodName + ":description:" + description + "%%%%%%");
            }
        }
    }

}
